import { defineStore } from "pinia";
import { Message } from "@arco-design/web-vue";
import * as dictBaseApi from "@/api/dict-base";

export const useDictBaseStore = defineStore("dictBase", () => {
  async function getDictGroupList(param?: any) {
    const response = await dictBaseApi.getDictGroupList(param);
    if (response.code === 20000) {
      return response.data.dictGroups;
    }
    return [];
  }

  async function saveDictGroup(param?: any) {
    const apiCall = param.id
      ? dictBaseApi.updateDictGroup
      : dictBaseApi.createDictGroup;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteDictGroup(param?: any) {
    const response = await dictBaseApi.deleteDictGroup(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  async function getDicts(param?: any) {
    const response = await dictBaseApi.getDicts(param);
    if (response.code === 20000) {
      return response.data.dicts;
    }
    return [];
  }

  async function saveDict(param?: any) {
    const apiCall = param.id ? dictBaseApi.updateDict : dictBaseApi.createDict;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteDict(param?: any) {
    const response = await dictBaseApi.deleteDict(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  return {
    getDictGroupList,
    saveDictGroup,
    deleteDictGroup,
    getDicts,
    saveDict,
    deleteDict,
  };
});
